package com.dz.lm.service.usersearch.impl;

import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.dao.userseach.write.UserSearchDao;
import com.dz.lm.domain.userproduct.UserProduct;
import com.dz.lm.domain.usersearch.UserSearch;
import com.dz.lm.domain.usersearch.UserSearchExample;
import com.dz.lm.service.usersearch.UserSearchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/8 17:22;
 */
@Service("userSearchService")
public class UserSearchServiceImpl implements UserSearchService {
    @Resource(name = "writeUserSearchDao")
    private UserSearchDao writeUserSearchDao;
    @Resource(name = "readUserSearchDao")
    private com.dz.lm.dao.userseach.read.UserSearchDao  readUserSearchDao;
    @Override
    @Transactional
    public int add(String content, int userId) throws ServiceException {
        int ret=0;
        UserSearch search = new UserSearch();
        search.setContent(content);
        search.setUserId(userId);
        search.setCreateTime(DateUtil.getTimeStr(new Date()));
        try {
            ret = writeUserSearchDao.insertSelective(search);
        } catch (DaoException e) {
        }
        return ret;
    }

    @Override
    public PageDto query(int page, int rows, int userId) throws ServiceException {
        PageDto pageDto = new PageDto();
        PageHelper.startPage(page,rows);
        UserSearchExample ex = new UserSearchExample();
        ex.setOrderByClause("create_time DESC");
        UserSearchExample.Criteria cr = ex.createCriteria();
        cr.andUserIdEqualTo(userId);
        try {
            List<UserSearch> list = readUserSearchDao.selectByExample(ex);
            if(list !=null && list.size()>0){
                PageInfo pageInfo = new PageInfo(list);
                pageDto.setList(list);
                pageDto.setInfo(pageInfo);
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return pageDto;
    }
}
